<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/5/9
 * Time: 11:33
 */

namespace App\Common\Auth;

use Lcobucci\JWT\Builder;
use Lcobucci\JWT\Parser;
use Lcobucci\JWT\Signer\Hmac\Sha256;
use Lcobucci\JWT\ValidationData;

/**
 * 单例 一次请求中所有出现使用jwt的地方都是一个用户
 * Class JwtAuth
 * @package App\Common\Auth
 */

class JwtAuth
{
    private $token;
    private $iss='127.0.0.1';
    private $aud='xxm.laravel-admin.com';
    private $uid;
    private $secrect='das@^%*()(*^&*!^(*';
    /**
     * 用户传上来的token
     * @var
     */
    private $decodeToken;
    /**
     * 单例模式 jwtAuth句柄
     * @var
     */
    private static $instance;

    /**
     * 获取jwt句柄
     * @return JwtAuth
     */
    public static function getInstance()
    {
        if(is_null(self::$instance))
        {
            self::$instance=new self();
        }
        return self::$instance;
    }

    /**
     * 禁止实例化
     * JwtAuth constructor.
     */
    private function __construct()
    {
    }

    /**
     * 禁止克隆
     */
    private function __clone()
    {
        // TODO: Implement __clone() method.
    }

    /**
     * 获取token
     * @return string
     */
    public function getToken()
    {
        return (string)$this->token;
    }

    /**
     * 设置token
     * @param $token
     * @return $this
     */
    public function setToken($token)
    {
        $this->token=$token;
        return $this;
    }

    /**
     * @param $uid
     * @return $this.
     */
    public function setUid($uid)
    {
        $this->uid=$uid;
        return $this;
    }
    /**
     * 编码jwt token
     * @return $this
     */
    public function encode()
    {
        $time=time();
        $this->token=(new Builder())
            ->setHeader('alg','HS256')
            ->setIssuer($this->iss)//设置发行人
            ->setAudience($this->aud)//设置接收人
            ->setIssuedAt($time)//设置生成token的时间
            ->setExpiration($time+3600)//设置过期
            ->set('id',$this->uid)//自定义值 用户id
            ->sign(new Sha256(),$this->secrect)
            ->getToken();
        return $this;
    }

    /**
     * @return \Lcobucci\JWT\Token
     */
    public function decode()
    {
        if(!$this->decodeToken){
            $this->decodeToken=(new Parser())->parse((string)$this->token);
            $this->uid=$this->decodeToken->getClaim('id');//获取id

        }

        return $this->decodeToken;
    }

    /**
     *验证token最后一个字符串是否一致
     * @return bool
     */
    public function verify()
    {
        $result=$this->decode()->verify(new Sha256(),$this->secrect);
        return $result;
    }

    /**
     *验证iss,aud....是否一致
     * @return bool
     */
    public function validate()
    {
        $data=new ValidationData();
        $data->setIssuer($this->iss);
        $data->setAudience($this->aud);
        return $this->decode()->validate($data);
    }
}